import 'package:aichat_flutter_wallet/module_common/widgets/token_widget/token20_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import '../../../module/module_trade/pages/trade_transactions.dart';
import '../../../module/module_wallet/widgets/row_delete_button.dart';
import '../../provider/token_list_provider.dart';
import '../../provider/wallet_info_provider.dart';
import '../public_tip/public_tip.dart';

class TokenListPage extends StatefulWidget {

  const TokenListPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _TokenListPageState();
  }
}

class _TokenListPageState extends State<TokenListPage> {
  late ValueNotifier<int> refreshRateNotifi;

  @override
  void initState() {
    super.initState();

    refreshRateNotifi = ValueNotifier<int>(1);
  }

  @override
  void dispose() {
    refreshRateNotifi.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<TokenListNotifier>(
      builder: (c, model, child) {
        return ListView.builder(
            padding: EdgeInsets.only(top: 0),
            physics: const ClampingScrollPhysics(),
            itemCount:
                context.watch<WalletInfoNotifier>().wallet?.needBackup() ??
                        false
                    ? model.tokenFungibleList.length + 2
                    : model.tokenFungibleList.length + 1,
            itemBuilder: (_, index) {
              if (0 == index) {
                return Token20Widget(
                  token: model.nativeToken,
                  onTap: () {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                      return TradeTransactionsPage(
                          contract: model.nativeToken!.contract.length > 0
                              ? model.nativeToken!.contract
                              : '1',
                          contractType: model.nativeToken!.contractType,
                          token: model.nativeToken!);
                    })).then((value) => context
                        .read<TokenListNotifier>()
                        .refreshTokenListFromServer());
                  },
                );
              }
              if ((context.watch<WalletInfoNotifier>().wallet?.needBackup() ??
                          false) ==
                      true &&
                  index == model.tokenFungibleList.length + 1) {
                return SizedBox(
                  height: 84.w,
                );
              }
              return Slidable(
                  //创建一个可以侧滑的组件
                  enabled: true,
                  key: Key(index.toString()),
                  child: Token20Widget(
                      token: model.tokenFungibleList[index - 1],
                      onTap: () {
                        var item = model.tokenFungibleList[index - 1];
                        Navigator.of(context)
                            .push(MaterialPageRoute(builder: (_) {
                          return TradeTransactionsPage(
                            contract: item.contract,
                            contractType: item.contractType,
                            token: model.tokenFungibleList[index - 1],
                          );
                        })).then((value) => context
                                .read<TokenListNotifier>()
                                .refreshTokenListFromServer());
                        // NavigationBar.of
                        // Router.
                      }),
                  endActionPane: ActionPane(
                      //尾部组件
                      extentRatio: 0.2,
                      motion: ScrollMotion(),
                      children: [
                        Expanded(
                          child: SizedBox(),
                        ),
                        deleteButton(
                          onTap: (deleteCTX) {
                            print('widget.loadingStatus status-----  ${model.isLoading}');
                            if(model.isLoading){
                              Toast.show(context,
                                  message:
                                  "The network is not fully loaded yet, please try again later.",
                                  status: '0',
                                  position: Dpositon.top);
                              Slidable.of(deleteCTX)?.close();
                              return;
                            }
                            Token token = model.tokenFungibleList[index - 1];
                            if (token.isCustom) {
                              context.read<TokenListNotifier>().deleteTokens(
                                  model.tokenFungibleList[index - 1]);
                            } else {
                              model.tokenFungibleList[index - 1].isAdded =
                                  !model.tokenFungibleList[index - 1].isAdded;
                              context.read<TokenListNotifier>().updateTokens(
                                  model.tokenFungibleList[index - 1]);
                            }

                            Slidable.of(deleteCTX)?.close();
                          },
                        )
                      ]));
            });
      },
    );
  }
}
